import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import {Headers, RequestOptions} from '@angular/http';
import {User} from "../entity/user";
import {Observable}     from 'rxjs/Observable';
import {AuthService} from "./auth.service";

@Injectable()
export class UserService {

  private httpUrl = 'http://localhost:8080/user';

  constructor(private http: Http) {}

  register(user: User): void {
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    this.http.post(this.httpUrl + "/register",
      user
      , options)
      .toPromise()
      .then((count) => {
        console.log(count);
      })
      .catch(this.handleError);
  }

  sendRetrievePasswordMail(phone:string): Observable<boolean> {
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    return this.http.get(`${this.httpUrl}/sendRetrievePasswordMail?phone=${phone}`, options)
      .map(this.extractData)
      .catch(this.handleError);
  }

  retrievePassword(token:string, password:string):Observable<number> {
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    return this.http.post(`${this.httpUrl}/retrievePassword`,{token, password}, options)
      .map(this.extractData)
      .catch(this.handleError);
  }

  currentUser(compactJws:string): Observable<User> {
    const headers = new Headers({
      'Content-Type': 'application/json',
      'authorization': compactJws
    });
    const options = new RequestOptions({headers: headers});
    return this.http.post(`${this.httpUrl}/secure/currentUser`,{} , options)
      .map(this.extractData)
      .catch(this.handleError);
  }

  login(phone:string, passWord:string): Observable<any> {
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});

    return this.http.post(this.httpUrl + "/login", {"phone": phone, "passWord": passWord}, options)
      .catch(this.handleError);
  }

  private extractData(res: Response) {
    const body = res.json();
    return body || {};
  }

  private handleError(error: any) {
    // In a real world app, we might use a remote logging infrastructure
    // We'd also dig deeper into the error to get a better message
    const errMsg = (error.message) ? error.message :
      error.status ? `${error.status} - ${error.statusText}` : 'Server error';
    console.error(errMsg); // log to console instead
    return Observable.throw(errMsg);
  }

  /*  insertActivity(aaid:string,title:string,uploader:string,synopsis:string,content:string):void {
   const headers = new Headers({ 'Content-Type': 'application/json' });
   const options = new RequestOptions({ headers: headers });

   this.http.post(this.httpUrl+"/insertActivity", { aaid,title,uploader,synopsis,content }, options).toPromise()
   .then(this.extractData)
   .catch(this.handleError);

   }*/

  /*  getActivitysAreas():Observable<ActivityArea[]> {
   return this.http.get(this.httpUrl+"/getActivityAreas")
   .map(this.extractData)
   .catch(this.handleError);
   }

   getActivity(id:string):Promise<Activity> {

   const headers = new Headers({ 'Content-Type': 'application/json' });
   const options = new RequestOptions({ headers: headers });

   return this.http.post(this.httpUrl+"/getActivity", { id }, options)
   .toPromise()
   .then(this.extractData)
   .catch(this.handleError);
   }*/
}
